# -*- coding: utf-8 -*-
from django.contrib.auth.models import User
from django.db import models
from pub.gridfsstorage import MyGridFSStorage
from pub.models import District, Cuisine, Dict, Bus_area
from uuid import uuid4

class Customer(models.Model):
    """
            消费者
    """
    user = models.ForeignKey(User,unique=True)
    nickname = models.CharField("昵称",max_length="50", blank=True, null=True)
    #头像
    avatar = models.ImageField(upload_to='/',storage=MyGridFSStorage(database='mongodb'),blank=True,null=True)
    #用户所在地区
    district = models.ForeignKey(District, verbose_name="消费者所在地区", blank=True, null=True)
    def __unicode__(self):
        return u'%s' %(self.nickname)
    
class Rest(models.Model):
    """
            餐厅
    """
    user = models.ForeignKey(User, unique=True)
    name = models.CharField("餐厅名称", max_length="50")
    #是餐厅主要url的组成部分 /rest/{{alias}}/
    alias = models.CharField("餐厅别名", max_length="50",blank=True, null=True)
    desc = models.TextField("餐厅描述", blank=True, null=True)
    img = models.ImageField(upload_to='/',storage=MyGridFSStorage(database='mongodb'),blank=True,null=True, verbose_name="餐厅主图")
    cuisine = models.ForeignKey(Cuisine, verbose_name="主打菜系", blank=True, null=True)
    bus_area = models.ForeignKey(Bus_area, verbose_name="商圈", blank=True, null=True)
    
    address = models.CharField("地址", max_length="50",blank=True, null=True)
    traffic_map = models.URLField("地图", max_length="50",blank=True, null=True)
    phone = models.CharField("联系电话", max_length="15",blank=True, null=True)
    spend = models.ForeignKey(Dict, verbose_name="人均消费",blank=True, null=True)
    open_time = models.CharField(verbose_name="开始营业时间", max_length="20", null=True)
    close_time = models.CharField(verbose_name="结束营业时间", max_length="20", null=True)
    mobile = models.CharField("接收短信手机号", max_length="11", blank=True, null=True)
    
    #餐厅状态 false=未通过审核  true=已通过审核 
    state = models.BooleanField(default=False)
    #权重越大显示在越前,相当于浏览者的关注度
    weight = models.PositiveSmallIntegerField(default=1)
    
    #用户订餐后是否自动确认用户订餐成功  True为自动确认，False为手动确认
    confirm_auto = models.BooleanField("是否自动确认订餐成功",default=False)
    def __unicode__(self):
        return u'%s' %(self.name)
    
class Rest_intro_img(models.Model):
    """
            餐厅介绍图
    """
    
    rest = models.ForeignKey(Rest)
    img = models.ImageField(upload_to='/',storage=MyGridFSStorage(database='mongodb'),blank=True, verbose_name="餐厅简介图")
    #权重越大显示在越前,相当于浏览者的关注度
    weight = models.PositiveSmallIntegerField(default=1)
    reg_time = models.DateTimeField(verbose_name="添加时间", auto_now_add=True)
    

class Rest_ad_img(models.Model):
    """
            餐厅广告图
    """
    
    rest = models.ForeignKey(Rest)
    img = models.ImageField(upload_to='/',storage=MyGridFSStorage(database='mongodb'),blank=True,verbose_name="餐厅简介图")
    ad_word = models.CharField("广告语", max_length="50",null=True)
    #权重越大显示在越前,相当于浏览者的关注度
    weight = models.PositiveSmallIntegerField(default=1)
    reg_time = models.DateTimeField(verbose_name="添加时间", auto_now_add=True)
    
class Rest_apply(models.Model):
    """
            餐厅加盟申请表
    """
    #申请用户
    user = models.ForeignKey(User,unique=True)
    name = models.CharField("餐厅名称", max_length="50")
    contact = models.CharField(verbose_name="联系人", max_length="10")
    phone = models.CharField(verbose_name="联系电话", max_length="15")
    bus_lic = models.ImageField(upload_to='/',storage=MyGridFSStorage(database='mongodb'),blank=True,null=True, verbose_name="营业执照")
    audited = models.BooleanField(default=False) 
    passed = models.BooleanField("是否通过审核",default=False)
    reg_time = models.DateTimeField(verbose_name="申请时间", auto_now_add=True)
    def __unicode__(self):
        return u'%s' %(self.name)

class Rest_cuis_type(models.Model):
    """
    '每个餐厅对应的菜式分类表
    """
    id = models.CharField(max_length=50, primary_key=True, default=uuid4)
    rest = models.ForeignKey(Rest)
    name = models.CharField("菜式分类", max_length="50")
    parent = models.ForeignKey("self", verbose_name="上一级菜式分类", blank=True, null=True)
    reg_time = models.DateTimeField(verbose_name="登记时间", auto_now_add=True)
    #排序权重
    weight = models.PositiveSmallIntegerField(default=1)
    level = models.PositiveSmallIntegerField(default=1,null=True) 
    def __unicode__(self):
        return u'%s' %(self.name)
    
class Common_dish(models.Model):
    """
            通用菜式表,用于记录普遍的菜式，方便用户录入,由客服维护
    """
    name = models.CharField("菜式名称", max_length="50")
    img = models.ImageField(upload_to='/',storage=MyGridFSStorage(database='mongodb'),blank=True,null=True, verbose_name="菜式图片")
    desc = models.TextField("菜式描述", blank=True, null=True)
    cuisine = models.ForeignKey(Cuisine, verbose_name="所属菜系", blank=True, null=True)
    propose_price = models.FloatField(verbose_name="建议价格",null=True)
    
    reg_time = models.DateTimeField(verbose_name="登记时间", auto_now_add=True)
    #排序权重
    weight = models.PositiveSmallIntegerField(default=1)
    def __unicode__(self):
        return u'%s' %(self.name)
    
    
class Rest_dish(models.Model):
    """
            餐厅菜式表
    """
    id = models.CharField(max_length=50, primary_key=True, default=uuid4)
    #菜式所属餐厅
    rest = models.ForeignKey(Rest)
    #菜式所属的餐厅自定义分类
    rest_cuis_type = models.ForeignKey(Rest_cuis_type,verbose_name="分类")
    name = models.CharField("菜式名称", max_length="50")
    img = models.ImageField(upload_to='/',storage=MyGridFSStorage(database='mongodb'),blank=True,null=True, verbose_name="菜式图片")
    desc = models.TextField("菜式描述", blank=True, null=True)
    cuisine = models.ForeignKey(Cuisine, verbose_name="所属菜系")
    price = models.FloatField(verbose_name="价格",null=True)
    special = models.BooleanField(verbose_name="是否特色菜", default=False)
    sign_dish_img = models.ImageField(upload_to='/',storage=MyGridFSStorage(database='mongodb'),blank=True,null=True, verbose_name="招牌菜广告图片")
    sign_dish_ad =  models.CharField(verbose_name="特色菜广告词", max_length=200,blank=True,null=True)
    #此菜式状态 false=下架无效  true=上架可点
    state = models.BooleanField("是否有效",default=True)
    
    
    #排序权重
    weight = models.PositiveSmallIntegerField(default=1)
    def __unicode__(self):
        return u'%s' %(self.name)

